//
//  ViewController.m
//  11-30TableView
//
//  Created by blue on 15-11-30.
//  Copyright (c) 2015年 hyd. All rights reserved.
//

#import "ViewController.h"
#import "inputViewController.h"
@interface ViewController ()
{

    NSMutableArray * names;
    NSMutableArray * tels;
  

}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    
    // Do any additional setup after loading the view, typically from a nib.
    
    
    self.title = @"电话簿";
   names = @[
              @"陈晨",
             @"陈翔",
              @"陈晓"
              
              ].mutableCopy;
    
    tels = @[
             @"132456789",
            @"135698745",
             @"158236978"
             
            ].mutableCopy;
    
//  self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
//    self.tableView.dataSource = self;
//    self.tableView.delegate = self;
//  [ self.view addSubview:self.tableView];
//
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(onEditing:)];


}

-(void)onEditing:(UIBarButtonItem *)btn
{
    [self.tableView setEditing:!self.editing animated:YES];
    btn.title = self.editing ? @"编辑":@"完成";
    [self.tableView reloadData];
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return tableView.isEditing ? names.count + 1 : names.count;

}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return indexPath.row - names.count? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert;


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    if (indexPath.row == names.count) {
        return cell;
    }
  cell.textLabel.text = names[indexPath.row];
    cell.textLabel.text = tels[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    inputViewController * con = [[inputViewController alloc]initWithStyle:UITableViewStyleGrouped];
    con.name = names[indexPath.row];
    con.tel = tels[indexPath.row];
    [self.navigationController pushViewController:con animated:YES];

}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
